package net.qhzw.vip.qiyu.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.qhzw.vip.qiyu.entity.Shop;
import net.qhzw.vip.qiyu.entity.SysRole;
import net.qhzw.vip.qiyu.entity.SysUser;
import net.qhzw.vip.qiyu.model.ModifyUserForm;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.ShopService;
import net.qhzw.vip.qiyu.service.SysUserService;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Api(tags = "系统用户管理")
@RestController
@RequestMapping("sys/users")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ShopService shopService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @ApiOperation(value = "查询系统用户")
    @GetMapping("{id}")
    public JsonResult<SysUser> getSysUser(@PathVariable Long id) {
        SysUser sysUser = sysUserService.getById(id);

        return JsonResult.ok(sysUser);
    }

//    public static void main(String[] args) {
//        for (int i = 0; i < 10; i++) {
//            System.out.println(IdWorker.getId());
//        }
//
//    }

    @ApiOperation(value = "分页查询系统用户")
    @GetMapping
    public JsonResult<Page<SysUser>> findByPage(Page<SysUser> page, String name) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        Page<SysUser> sysUserPage = sysUserService.findByPage(page, name, shopId);
        return JsonResult.ok(sysUserPage);
    }

    @ApiOperation(value = "添加系统用户")
    @PostMapping
    public JsonResult addSysUser(@RequestBody SysUser sysUser) {
        Long userId = SecurityUtils.getUserId();
        boolean isOK = sysUserService.addSysUser(userId, sysUser);
        if (isOK) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "修改系统用户")
    @PutMapping
    public JsonResult updateSysUser(@RequestBody SysUser sysUser) {
        boolean isOK = sysUserService.updateSysUser(sysUser);
        if (isOK) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "设置用户状态用户")
    @PutMapping("updateStatus")
    public JsonResult updateStatus(@RequestBody SysUser sysUser) {
        boolean isOK = sysUserService.updateSysUser(sysUser);
        if (isOK) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "删除系统用户")
    @DeleteMapping("{id}")
    public JsonResult<SysUser> deleteSysUser(@PathVariable String id) {
        List<String> strings = Arrays.asList(id.split(","));
        boolean remove = sysUserService.removeByIds(strings);
        if (remove) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @ApiOperation(value = "resetPassword")
    @PutMapping("resetPassword/{id}")
    public JsonResult resetPassword(@PathVariable Long id) {
        SysUser sysUser = new SysUser();
        sysUser.setId(id);
        sysUser.setPassword(new BCryptPasswordEncoder().encode("123456"));
        boolean update = sysUserService.updateSysUser(sysUser);
        if (update) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "个人信息修改")
    @PutMapping("updateUserInfo")
    public JsonResult updateUserInfo(@RequestBody ModifyUserForm modifyUserForm) {
        boolean update = sysUserService.updateUserInfo(modifyUserForm);
        if (update) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "根据token获取用户详情")
    @GetMapping("getAdminInfo")
    public JsonResult<SysUser> getAdminInfo() {
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.getById(userId);
        //查询角色
        if (sysUser != null && sysUser.getRoleId() != null) {
            // String roleName = sysUserService.getRoleNameById(sysUser.getRoleId());

            SysRole sysRole = sysUserService.getRoleById(sysUser.getRoleId());
            sysUser.setRoleName(sysRole.getName());
            sysUser.setRoleCode(sysRole.getCode());
        }
        if (sysUser != null && sysUser.getCompanyId() != null) {
            String companyName = sysUserService.findCompanyNameById(sysUser.getCompanyId());
            sysUser.setCompanyName(companyName);
        }
        if (sysUser.getShopId() != null) {
            Shop shop = shopService.getById(sysUser.getShopId());
            sysUser.setShopType((shop != null && shop.getShopType() != null) ? shop.getShopType() : null);
        }
        //获取权限数据
        //根据userId 获取权限数据
        List<String> permissions = sysUserService.getPermissions(userId);
        sysUser.setPermissions(permissions);

        long expiresIn = 7 * 24 * 60 * 60 * 1000;
        redisTemplate.opsForValue().set("adminLogin_" + userId, JSON.toJSONString(sysUser), expiresIn, TimeUnit.SECONDS);
        return JsonResult.ok(sysUser);
    }


    @ApiOperation(value = "系统用户退出登录")
    @GetMapping("logout")
    public JsonResult logout() {
        return JsonResult.ok();
    }


}
